package quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.time.LocalDateTime;

/**
 * <h1>定时器 Quartz</h1>
 *
 * @author zhh 2019-05-31
 */
public class QuartzExample {
    public static void main(String[] args) throws SchedulerException {
        // 创建一个 JobDetail 的实例，将该实例与 TimerJob.class 绑定
        // JobDetail jobDetail = JobBuilder.newJob(TimerJob.class).withIdentity("myJob").build();
        JobDetail jobDetail = JobBuilder.newJob(TimerJob.class)
                .withIdentity("muJob")
                .usingJobData("message", "hello my job1")
                .usingJobData("floatJobValue", 3.14F)
                .build();
        System.out.println("jobDetail's name:" + jobDetail.getKey().getName());
        System.out.println("jobDetail's group:" + jobDetail.getKey().getGroup());
        System.out.println("jobDetail's jobClass:" + jobDetail.getJobClass().getName());
        // 传建一个 Trigger 实例，定义该 job 立即执行，并且每隔2秒钟重复执行一次，直到永远
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("myTrigger", "group1")
                .usingJobData("message", "hello my trigger1")
                .usingJobData("doubleTriggerValue", 2.0d)
                .startNow()
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule()
                                .withIntervalInSeconds(2)
                                .repeatForever()
                ).build();
        // 创建 Schedule 实例
        StdSchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
        // 打印当前的执行时间
        System.out.println("Current Exec Time is: " + LocalDateTime.now());
        scheduler.scheduleJob(jobDetail, trigger);
    }
}
